// pages/gujiquan/index.js
var app = getApp(), fetchApi = require("../../../../utils/httpPost.js");
import Toast from '../../../../component/vant/toast/toast';
import Dialog from '../../../../component/vant/dialog/dialog';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    chat:{},
    loading: false,
    netWork: false,
    isLoading: true,
    hasData:false,
    pageIndex:1,
    pageSize:10,
    faceUrl:'',
    parent_id:'',
    isForbidden:0,
    chat_id:0,
    isiphonex: app.globalData.isiphonex,
    isxiaomi: app.globalData.isxiaomi,
    appraiseContent:'',
    to_user_id:0,
    appraises:[],
    actions: [
      { name: '编辑',  },
      { name: '删除',  color: '#ee0a24'},
    ],
    showAction:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id){
      this.data.chat_id = options.id;
    }
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/estate/chat/detail', {
      chat_id: that.data.chat_id,
    }, function (res) {
      console.log('物业经理接待日数据',res)
      if (res.data.code == 0) {
        that.setData({
          chat: res.data.data.chat,
          nickName:res.data.data.nickName,
          faceUrl:res.data.data.faceUrl
        })
      }
      that.setData({ isLoading: false, loading: true, netWork: true })
    }, function () {
        that.setData({  isLoading: false,  loading: true,  netWork: true, }) 
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.pageIndex = 1
    this.initOp()
  },
  goAdd(){
    wx.navigateTo({
      url: '/pages/gujiquan/add/index',
    })
  },
  appraiseInput(e) {
      console.log('appraiseInput', e)
      var content = e.detail.value;
      var chatId = e.currentTarget.dataset.chatid;
      this.data.chat_id = chatId;
      this.data.appraiseContent = content;
  },
  
    // 评论
    pubAppraise(e) {
        console.log('pubAppraise', e)
        var content = e.detail.value;
        var chatId = e.currentTarget.dataset.chatid;
        this.data.chat_id = chatId;
        this.data.appraiseContent = content;
        this.data.to_user_id = this.data.chat.userId;
        this.onHttpPublishOp(chatId, this.data.to_user_id);
    },
    onHttpPublishOp: function (chat_id, to_user_id) {
        var that = this
        fetchApi.httpPost('/estate/chat/add/apply', {
            chat_id: chat_id,
            to_user_id: to_user_id,
            parent_id: that.data.parent_id,
            content: that.data.appraiseContent,
        }, function (res) {
            console.log("发布评论返回数据", res)
            if (res.data.code == 0) {
                var applies = res.data.data.applies;
                that.data.chat.chatApplies = applies;
                that.data.chat.placeholder = '我也说一句~';
                that.data.appraiseContent = '';
                that.data.gujiId = '';
                that.data.appraiseId = '';
                that.data.parent_id = '';
                that.data.gujiIndex = 0;
                that.setData({
                  chat: that.data.chat,
                    appraiseContent: '',
                })
                Toast.success('评论成功');
            } else {
                Toast.fail(res.data.msg);
            }
        }, function () {})
    },
    appraiseAplay(e) {
        console.log('appraiseAplay', e);
        var id = e.currentTarget.dataset.id;
        var appraiseIndex = e.currentTarget.dataset.appraiseIndex;

        var appraise = this.data.chat.appraises[appraiseIndex];

        var nickName = appraise.nickName;
        console.log('nickName', nickName);

        this.data.chat.placeholder = "回复" + nickName;
        console.log('this.data.gujis', this.data.gujis)

        this.data.appraiseId = appraise.id;
        this.data.parent_id = appraise.id;
        this.data.to_user_id = appraise.userId;
        this.setData({
          chat: this.data.chat
        })
    },
  initOp:function(){
    console.log('initOp')
    this.setData({
      currentTab: 0,
      loading: false,
      netWork: false,
      isLoading: true,
      hasData:false,
      gujis:[]
    })
    this.onHttpInit()
  },
  clickImg:function(e){
    console.log(e)
    wx.previewImage({
      urls: [e.currentTarget.dataset.imgurl],
      success() {
      },
      faile() {
      },
      complete() {
      }
    })
  },
  
  goDelInfo(e){
    var id = e.currentTarget.dataset.id
    var message = "是否要删除该发布信息？";
    var that = this
    Dialog.confirm({
      title: '删除发布',
      message
    }).then(() => {
      that.onHttpDeleteOp(id)
    })
    .catch(() => {
      Dialog.close();
    });
  },
  
  onHttpDeleteOp: function (id){
    var that = this;
    fetchApi.httpPost('/commonInfo/delete', {
      chat_id:id,
    }, function (res) {
      console.log("删除返回数据", res)
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
        setTimeout(function(){
          wx.navigateBack({
            delta: -1,
          })
        },500)
      } else {
        Toast.fail(res.data.msg);
      }
      that.refreshRecord()
    },function(){})
  },

  goManager(){
    this.setData({
      showAction : ! this.data.showAction
    })
  },
  onChangeSheet(e){
    var that = this
    console.log('onChangeSheet',e)
    var name = e.detail.name;
    if(name == '编辑'){
      this.goManager();
      wx.navigateTo({
        url: '/pages/commonInfo/addCar/index?id=' + this.data.chat_id,
      })
    }else if(name == '删除'){
      var message = "是否要删除该发布信息？";
      Dialog.confirm({
        title: '删除发布',
        message
      }).then(() => {
        that.onHttpDeleteOp(this.data.chat_id)
      })
      .catch(() => {
        Dialog.close();
      });
    }
  }
})